library(tidyverse)

# Hierarchy: Death Penalty, Corporal Punishment, Punitiveness,
#            Fear of Crime, Security Behaviour, Satisfaction

# Within each: All, Male, Female, 
#              16-24, 25-49, 50-64, 65+, 
#              HAS NO RACE VARIABLE

# Varname, Date, Index, Demographic, Poll, Question

######### YouGov ###########

l1 <- list.files("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Gallup and YouGov/YouGov Trackers/Finished/")

readin <- function (x) {
  url <- paste0("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Gallup and YouGov/YouGov Trackers/Finished/",x)
  var1 <- str_split_i(x, "_", 1)
  demo1 <- str_split_i(x, "_", 2)
  read_csv(url) %>%
    mutate(var = var1,
           demographic = substring(demo1, 1, nchar(demo1)-4)) -> df1
  return(df1)
}
output <- lapply(l1, readin)

splitnflatten <- function (x) {
  temp <- bind_rows(output[((x*7)+1):((x*7)+7)])
  return(temp)
}
result <- lapply(c(0:11), splitnflatten)


result[[1]] %>%
  mutate(Index = `Not very much confidence` + `No confidence at all`,
         Varname = "YG_NoConfInPolice",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Generally speaking how much confidence do you have in the police to deal with crime in your local area?") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/Satisfaction.csv", 
              sep = ",", append = FALSE, quote = FALSE, 
              col.names = TRUE, row.names = FALSE)

result[[2]] %>%
  mutate(Index = `Gone up`,
         Varname = "YG_CrimeUpLocal",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Do you think the level of crime in your LOCAL area has gone up or down over the last few years or has it stayed the same?") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/FearCrime.csv", 
              sep = ",", append = FALSE, quote = FALSE, 
              col.names = TRUE, row.names = FALSE)

result[[3]] %>%
  mutate(Index = `Gone up`,
         Varname = "YG_CrimeUpNational",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Do you think the level of crime NATIONALLY has gone up or down over the last few years or has it stayed the same?") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/FearCrime.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[4]] %>%
  mutate(Index = `Would support the death penalty for this`,
         Varname = "YG_Death_AllMurder",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Would you support or oppose the reintroduction of the death penalty for the following crimes? All cases of murder") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/DeathPenalty.csv", 
              sep = ",", append = FALSE, quote = FALSE, 
              col.names = TRUE, row.names = FALSE)

result[[5]] %>%
  mutate(Index = `Would support the death penalty for this`,
         Varname = "YG_Death_ChildMurder",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Would you support or oppose the reintroduction of the death penalty for the following crimes? Murder of a child") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/DeathPenalty.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[6]] %>% 
  mutate(Index = `Would support the death penalty for this`,
         Varname = "YG_Death_MultiMurder",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Would you support or oppose the reintroduction of the death penalty for the following crimes? Multiple murders") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/DeathPenalty.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[7]] %>% 
  mutate(Index = `Would support the death penalty for this`,
         Varname = "YG_Death_PoliceMurder",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Would you support or oppose the reintroduction of the death penalty for the following crimes? Murder of a police officer") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/DeathPenalty.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[8]] %>%
  mutate(Index = `Would support the death penalty for this`,
         Varname = "YG_Death_TerrorMurder",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Would you support or oppose the reintroduction of the death penalty for the following crimes? Terrorist murder") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/DeathPenalty.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[9]] %>%
  mutate(Index = `Very effective` + `Fairly effective`,
         Varname = "YG_PrisonEffective",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "How effective if at all do you think the following sentences are at reducing crime in the future? Prison sentences") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/Punitiveness.csv", 
              sep = ",", append = FALSE, quote = FALSE, 
              col.names = TRUE, row.names = FALSE)

result[[10]] %>%
  mutate(Index = `Not harsh enough`,
         Varname = "YG_SentencingLevel",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Generally speaking do you think the sentences that the courts hand down to people who have been convicted of crimes are too harsh not harsh enough or get the balance about right?") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/Punitiveness.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)

result[[11]] %>%
  mutate(Index = `To punish criminals`,
         Varname = "YG_SentencingPurpose",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "When courts are deciding what punishment to give to someone who has committed a crime what do you think should be the main purpose of the sentence? To punish criminals") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/Punitiveness.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)


result[[12]] %>%
  mutate(Index = `Not very safe` + `Not safe at all`,
         Varname = "YG_WalkStreet",
         Date = as.Date(ISOdate(Year, 1, 1)),
         Demographic = demographic,
         n = n,
         Poll = "YouGov",
         Question = "Generally speaking how safe or unsafe do you feel walking on the street during the day in your area?") %>%
  select(Varname, Date, Index, Demographic, n, Poll, Question) %>%
  write.table(file = "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/YouGov/FearCrime.csv", 
              sep = ",", append = TRUE, quote = FALSE, 
              col.names = FALSE, row.names = FALSE)
